package com.eightywork.life.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.eightywork.life.R;
import com.eightywork.life.activity.BrowserActivity;
import com.eightywork.life.activity.ContentActivity;
import com.eightywork.life.activity.IndexActivity;
import com.eightywork.life.activity.PmDetailActivity;
import com.eightywork.life.activity.SpaceActivity;
import com.eightywork.life.imageCache.ImageLoader;
import com.eightywork.life.service.BaseService;
import com.eightywork.life.service.DataService;

public class MsgListAdapter extends BaseAdapter{
	private LayoutInflater pInflater;
	private Context context;
	private int mResource  = R.layout.item_feed_msg;
	private ImageLoader imageLoader; 
	List<Map<String, Object>> mListItems;
	public MsgListAdapter(Context context, List<Map<String, Object>> data) {
		this.mListItems = data;
		this.context = context;
		pInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);	
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return (mListItems != null) ? mListItems.size(): 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		final Map<String, Object> map = mListItems.get(position);
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = pInflater.inflate(mResource, parent, false);
			holder.avatar = (ImageView)convertView.findViewById(R.id.authorvavatar);
			holder.name = (TextView)convertView.findViewById(R.id.authorname);
			holder.msg = (TextView)convertView.findViewById(R.id.msg);
			holder.label = (View)convertView.findViewById(R.id.pm_label);
			holder.verify_type = (ImageView)convertView.findViewById(R.id.verify_type);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder)convertView.getTag();
		}
		if (map.get("mark").equals("notice")) {
			holder.label.setBackgroundColor(context.getResources().getColor(R.color.dark_red));
			imageLoader.DisplayImage((String)map.get("authoravatar"), holder.avatar, 0, 0, 0);
			holder.verify_type.setBackgroundResource(DataService.getVerifyTypeRes((String)map.get("verify_type")));
			holder.name.setText((String)map.get("authorname"));
			holder.msg.setText((String)map.get("note"));
		} else {
			holder.label.setBackgroundColor(context.getResources().getColor(R.color.purple));
			imageLoader.DisplayImage((String)map.get("toavatar"), holder.avatar, 0, 0, 0);
			holder.name.setText((String)map.get("authorname"));
			holder.verify_type.setBackgroundResource(DataService.getVerifyTypeRes((String)map.get("toVerity_type")));
			holder.msg.setText((String)map.get("message"));
		}
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				holder.label.setVisibility(View.GONE);
				if (map.get("mark").equals("notice")) {
	//				System.out.println("type ----->" + (String)map.get("type"));
					if (((String)map.get("type")).equals("talkcomment") || ((String)map.get("type")).equals("forward")) {
						Intent intent = new Intent(context, ContentActivity.class);
						intent.putExtra("id", (String)map.get("id"));
						intent.putExtra("idtype", "talkid");
					//	intent.putExtra("pushType", "notifce");
						intent.putExtra("isFromNotice", true);
						context.startActivity(intent);
					} else if (((String)map.get("type")).equals("friend")) {
						Intent intent = new Intent(context, SpaceActivity.class);
						intent.putExtra("uid", (String)map.get("uid"));
						context.startActivity(intent);
					} else {
						Intent intent = new Intent(context,IndexActivity.class);
						context.startActivity(intent);
					}
				} else {
					Intent intent = new Intent(context, PmDetailActivity.class);
					intent.putExtra("touid", (String)map.get("touid"));
					intent.putExtra("toavatar", (String)map.get("toavatar"));
					intent.putExtra("toverify_type", (String)map.get("toVerity_type"));
					context.startActivity(intent);
				}
			}
		});
		return convertView;
	}
	
	public final class ViewHolder {
		private ImageView avatar;
		private ImageView verify_type;
		private TextView name;
		private TextView msg;
		private View label;
	}

}
